//
//  IconSelectController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/12.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "IconSelectController.h"
#import "SceneIconCell.h"
#import "SceneIconModel.h"
#import "SureView.h"
#import "YHMarcros.h"
@interface IconSelectController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray <SceneIconModel *>* icons;
@property (nonatomic, strong) SceneIconCell *selectedCell;
@property (nonatomic, strong) SureView *sureView;

@end

@implementation IconSelectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择场景图标";
    [self.view addSubview:self.sureView];
    [self.sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.bottom.equalTo(self.view);
        make.height.equalTo(kIphoneSize_Height(70));
    }];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kIphoneSize_Height(10));
        make.trailing.leading.equalTo(self.view);
        make.bottom.equalTo(self.sureView.top);
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.icons.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SceneIconCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([self class]) forIndexPath:indexPath];
    cell.icon = self.icons[indexPath.item];
    if (indexPath.row == self.m_integerSelectedIndex) {
        cell.iconBtn.selected = YES;
        self.selectedCell = cell;
    }else {
        cell.iconBtn.selected = NO;
    }
    return cell;
}


- (void)setOrignalSlectedIcon{

    debugLogInfo(@"m_index= %zd",_m_integerSelectedIndex);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.m_integerSelectedIndex inSection:0];
    self.selectedCell.iconBtn.selected = NO;
    SceneIconCell *cell = (SceneIconCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    cell.iconBtn.selected = YES;
    

    self.selectedCell = cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.m_integerSelectedIndex = indexPath.row;
    [self.collectionView reloadData];
}
- (void)setM_integerSelectedIndex:(NSInteger)m_integerSelectedIndex
{
    _m_integerSelectedIndex = m_integerSelectedIndex;
}
#pragma mark - lazy loading
- (UICollectionViewFlowLayout *)flowLayout
{
    if (!_flowLayout) {

        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.itemSize = CGSizeMake(kScreenWidth / 3, kScreenWidth / 3);
    }
    return _flowLayout;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[SceneIconCell class] forCellWithReuseIdentifier:NSStringFromClass([self class])];
        _collectionView.backgroundColor = kBgColor;
    }
    return _collectionView;
}

- (SureView *)sureView
{
    if (!_sureView) {
        _sureView = [[SureView alloc]init];
        __weak typeof(self)weakSelf = self;
        _sureView.sureBtnClickBlock = ^() {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (!strongSelf) {
               debugLogInfo(@"strongSelf被释放了 ！");
                return ;
            }
            if (strongSelf.block) {
                strongSelf.block(strongSelf.m_integerSelectedIndex);
            }
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _sureView;
}
- (NSArray<SceneIconModel *> *)icons
{
    if (!_icons) {
        _icons = [SceneIconModel loadAllIcon];
        
    }
    return _icons;
}
@end
